// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import fs from 'fs';
import path from 'path';
import { isValidCppClassName } from './stringformal';
import  * as newqtfile from './newqtfile';
import  * as newcppfile from './newcppfile';

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Congratulations, your extension "newqtui" is now active!');

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with registerCommand
    // The commandId parameter must match the command field in package.json
    const disposable = vscode.commands.registerCommand('newqtui.newqtui', async (uri: vscode.Uri) => {
        // The code you place here will be executed every time your command is executed
        // Display a message box to the user
        const selectPath = uri.fsPath;
        // console.log(fs.lstatSync(selectPath));
        // console.log(selectPath);
        if(!fs.lstatSync(selectPath).isDirectory())
        {	
            return;
        }

        //!文件名输入
        const className = await vscode.window.showInputBox({
            prompt: '请输入类名',
            placeHolder: '类名'
        });

        if (!className) {
            // vscode.window.showErrorMessage(`${className}`);
            return;
        } else {
            const isValid: boolean = isValidCppClassName(className!.toString());
            if (!isValid) {
                vscode.window.showErrorMessage(`${className}名字不合法!`);
                return;
            }
        }
        
        const fileName: string = className.toLowerCase();
        const headFileName = path.join(selectPath,fileName+".h");
        const sourceFileName = path.join(selectPath,fileName+".cpp");
        const uiFileName = path.join(selectPath,fileName+".ui");

        //!下拉输入
        const baseClasses = ['QWidget', 'QMainWindow', 'QDialog'];
        const selectedBaseClass = await vscode.window.showQuickPick(baseClasses, {
            placeHolder: '选择继承的类'
        });
        if (!selectedBaseClass) {
            return;
        }
        if (fs.existsSync(headFileName)) { 
            vscode.window.showErrorMessage(`类:${className}已经存在,不能新建!`);
            return;
        };

        var info = newqtfile.newUiFile(uiFileName,className,selectedBaseClass);
        info = newqtfile.newHeadFile(headFileName,className,selectedBaseClass);
        info = newqtfile.newSourceFile(sourceFileName,className,selectedBaseClass);

        const confirm = await vscode.window.showInformationMessage(
            `类名: ${className}, 继承类: ${selectedBaseClass}`
        );
    });
    context.subscriptions.push(disposable);

    const newCppClass = vscode.commands.registerCommand('newqtui.newcppclass', async (uri: vscode.Uri) => {
        const selectPath = uri.fsPath;

        if(!fs.lstatSync(selectPath).isDirectory())
        {	
            return;
        }
        const className = await vscode.window.showInputBox({
            prompt: '请输入类名',
            placeHolder: '类名'
        });

        if (!className) {
            return;
        } else {
            const isValid: boolean = isValidCppClassName(className!.toString());
            if (!isValid) {
                vscode.window.showErrorMessage(`${className}名字不合法!`);
                return;
            }
        }
        
        const fileName: string = className.toLowerCase();
        const headFileName = path.join(selectPath,fileName+".h");
        const sourceFileName = path.join(selectPath,fileName+".cpp");


        var info = newcppfile.newHeadFile(headFileName,className);
        info = newcppfile.newSourceFile(sourceFileName,className);

        const confirm = await vscode.window.showInformationMessage(
            `类名: ${className}`
        );
    });
    context.subscriptions.push(newCppClass);


    const newHeadFile = vscode.commands.registerCommand('newqtui.newheadfile', async (uri: vscode.Uri) => {
        const selectPath = uri.fsPath;
        if(!fs.lstatSync(selectPath).isDirectory())
        {	
            return;
        }
        const className = await vscode.window.showInputBox({
            prompt: '请输入文件名',
            placeHolder: '文件名'
        });

        if (!className) {
            return;
        } else {
            const isValid: boolean = isValidCppClassName(className!.toString());
            if (!isValid) {
                vscode.window.showErrorMessage(`${className}名字不合法!`);
                return;
            }
        }
        
        const fileName: string = className.toLowerCase();
        const headFileName = path.join(selectPath,fileName+".h");
        var info = newcppfile.newHeadFile(headFileName,className);

        const confirm = await vscode.window.showInformationMessage(
            `文件名: ${fileName}+".h"`
        );
    });
    context.subscriptions.push(newHeadFile);

    const newSourceFile = vscode.commands.registerCommand('newqtui.newsourcefile', async (uri: vscode.Uri) => {
        const selectPath = uri.fsPath;
        if(!fs.lstatSync(selectPath).isDirectory())
        {	
            return;
        }
        const className = await vscode.window.showInputBox({
            prompt: '请输入文件名',
            placeHolder: '文件名'
        });

        if (!className) {
            return;
        } else {
            const isValid: boolean = isValidCppClassName(className!.toString());
            if (!isValid) {
                vscode.window.showErrorMessage(`${className}名字不合法!`);
                return;
            }
        }
        
        const fileName: string = className.toLowerCase();
        const headFileName = path.join(selectPath,fileName+".cpp");
        var info = newcppfile.newSourceFile(headFileName,className);

        const confirm = await vscode.window.showInformationMessage(
            `文件名: ${fileName}+".cpp"`
        );
    });
    context.subscriptions.push(newHeadFile);
}

// This method is called when your extension is deactivated
export function deactivate() { }
